
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta name="author" content="Yb">
    <title></title>
    <script src="../js/jquery-3.1.1.js"></script>
    <style>
        ul,
        li {
            margin: 0;
            padding: 0;
            list-style: none;
        }
        
        #box {
            width: 520px;
            height: 280px;
            margin: 100px auto;
            position: relative;
            font-family: 'Microsoft YaHei';
        }
        
        #box .list li {
            position: absolute;
            top: 0;
            left: 0;
            display: none;
        }
        
        #box .list .current {
            display: block;
        }
        
        #box .count {
            position: absolute;
            left: 10px;
            bottom: 10px;
        }
        
        #box .count li {
            float: left;
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background-color: #fa0;
            text-align: center;
            color: white;
            line-height: 20px;
            margin-left: 10px;
            opacity: 0.8;
            cursor: pointer;
        }
        
        #box .count li.current {
            background-color: #f60;
            opacity: 1;
        }
        .btn{
            position: absolute;
            right: 10px;
            bottom: 10px;
        }
        .btn>button{
            font-family: 'Microsoft YaHei';
            border: none;
            background-color: rgba(0, 0, 0, 0.3);
            outline: none;
        }
        .btn>button:hover{
            cursor: pointer;
            color: #fff;
        }
    </style>
</head>

<body>
    <div id="box">
       

        <ul class="list">
            <li class="current"><img src="img/1.jpg" alt=""></li>
            <li><img src="img/2.jpg" alt=""></li>
            <li><img src="img/3.jpg" alt=""></li>
            <li><img src="img/4.jpg" alt=""></li>
            <li><img src="img/5.jpg" alt=""></li>
        </ul>
        <ul class="count">
            <li class="current">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
        </ul>
        <div class="btn">
             <button id="btn1">&lt;</button>
             <button id="btn2">&gt;</button>
        </div>

    </div>
</body>

</html>
<script>
    var box = $('#box');
    var ali = $('.list li');
    var anum = $('.count li');
    var currentPage = 1;
	//总页数
	var pages = 4;
	var img = $('img');

    // 记录当前显示的图片的索引
    var current = 0;
    // 保存定时器
    var timer = null;
    // 先写自动的
    timer = setInterval(autoPlay, 1000);

    function autoPlay() {
        current++;
        if (current >= ali.length) {
            current = 0;
        }
        show(current);
    }

    // 手动的 
    for (var i = 0; i < anum.length; i++) {
        // 存储下标
        anum[i].index = i;
        anum[i].onmouseover = function () {
            show(this.index);
        }
    }

    // 处理图片的显示和隐藏
    function show(index) {
        current = index;
        $('.list li')
            .eq(index)
            .css('display', 'block')
            .siblings()
            .css('display', 'none');
        $('.count li')
            .eq(index)
            .addClass('current')
            .siblings()
            .removeClass();
    }
    // 当鼠标移入box容器的时候让自动播放停止 当鼠标移出的时候继续自动播放
    box[0].onmouseover = function () {
        clearInterval(timer);
    }
    box[0].onmouseout = function () {
        timer = setInterval(autoPlay, 1000);
    }
    


    // 添加按钮
    $('#btn1').click(function(){
        current--;
        show(current);
    });

    $('#btn2').click(function(){
        current++;
        show(current);
    });
</script>